import Vue from 'vue'
import Router from 'vue-router'
import layout from "@/layout/index";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getToken} from '@/utils/auth'
import store from './store'
import {Message} from 'element-ui'

Vue.use(Router)

NProgress.configure({showSpinner: false})

export const routeArr = [
    {
        path: '/redirect',
        component: layout,
        hidden: true,
        children: [
            {
                path: '/redirect/:path(.*)',
                component: (resolve) => require(['@/views/redirect'], resolve)
            }
        ]
    },
    {
        path: '',
        component: layout,
        redirect: 'index',
        hidden: true,
    },
    {
        path: "/",
        component: layout,
        hidden: false,
        meta: {title: '',menuType: 2},
        children: [
            {
                path: '/index',
                component: (resolve) => require(['@/views/index'], resolve),
                name: 'gen',
                meta: {title: '首页',icon:'el-icon-s-home',menuType: 2}
            }
        ]
    },
    {
        path: "/login",
        name: "login",
        hidden: true,
        meta: {
            title: "登录"
        },
        component: () => import('./views/login/index.vue')
    },
    {
        path: "/",
        component: layout,
        hidden: true,
        meta: {
            menuType: 2
        },
        children: [
            {
                path: '/404',
                component: (resolve) => require(['@/views/error/404'], resolve),
                name: '404',
                meta: {title: '404'}
            }
        ]
    },
    {
        path: "/",
        component: layout,
        hidden: true,
        meta: {
            menuType: 2
        },
        children: [
            {
                path: '/403',
                component: (resolve) => require(['@/views/error/403'], resolve),
                name: '403',
                meta: {title: '403'}
            }
        ]
    },
    {
        path: '',
        component: layout,
        redirect: 'index',
        hidden: true,
    },
    {
        path: "/",
        component: layout,
        hidden: true,
        meta: {
            menuType: 2
        },
        children: [
            {
                path: '/user/center',
                component: (resolve) => require(['@/views/sys/user/info/index'], resolve),
                name: 'UserInfo',
                meta: {title: '个人中心'}
            }
        ]
    },
];

let router = new Router({
    // base: '/cow_owner/',
    mode: 'history', // 去掉url中的#
    scrollBehavior: () => ({y: 0}),
    routes: routeArr
})

// 路由守卫
router.beforeEach((to, from, next) => {
    NProgress.start();
    if (getToken()) {
        // 防止重复登录
        if (to.path === '/login') {
            Message({
                message: "请勿重复登录!",
                type: 'error'
            })
            return next({name: from.name ? from.name : '/index'})
        } else {
            if (store.getters.roles.length === 0) {
                // 获取用户信息
                store.dispatch('GetInfo').then(res => {
                    // 拉取user_info
                    const roles = res.roles
                    store.dispatch('GenerateRoutes', {roles}).then(accessRoutes => {
                        // 根据roles权限生成可访问的路由表
                        router.addRoutes(accessRoutes) // 动态添加可访问路由表
                        next({...to, replace: true}) // hack方法 确保addRoutes已完成
                    })
                }).catch(err => {
                    store.dispatch('LogOut').then(() => {
                        Message({
                            message: "登录时间失效，请重新登录！",
                            type: 'warning'
                        })
                        next({path: '/login'})
                    })
                })
            } else {
                next()
            }
        }
    } else {
        if (to.path === '/login') {
            return next()
        } else {
            // 未登录
            Message({
                message: "请先登录!",
                type: 'error'
            })
            next({name: "login"})
        }
    }
    NProgress.done()
})

router.afterEach(() => {
    NProgress.done()
})

export default router
